package ynu.edu.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.edu.common.service.LogicService;
import ynu.edu.dao.*;
import ynu.edu.entity.*;


import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class BusinessService extends LogicService<BusinessDao, Business,Long> {

    public BusinessService(@Autowired BusinessDao dao){
        super(dao);
    }
    @Resource
    ShopItemDao shopItemDao;
    @Resource
    ShopDao shopDao;
    @Resource
    OrderDao orderDao;
    @Resource
    ItemDao itemDao;
    public List<Order1> getAllMyOrder(Long shopId){
        Shop shop=shopDao.getReferenceById(shopId);
        return orderDao.getAllMyOrder(shopId);
    }
    public List<Item> getAllMyItem(String name){
        Business business=getDAO().findBusinessByName(name);
        return itemDao.findAllByBusinessId(business.getId());
    }
    public List<Shop> getAllMyShop(String name){
        Business business=getDAO().findBusinessByName(name);
        return shopDao.findAllByBusinessId(business.getId());
    }
    public Order1 cancelOrder(Long shopId,Long orderId){
        Shop shop=shopDao.getReferenceById(shopId);
        Order1 order=orderDao.getReferenceById(orderId);
        shop.cancelOrder(order);
        shopDao.save(shop);
        return order;
    }
    public Order1 confirmOrder(Long shopId,Long orderId){
        Shop shop=shopDao.getReferenceById(shopId);
        Order1 order=orderDao.getReferenceById(orderId);
        shop.confirmOrder(order);
        shopDao.save(shop);
        return order;
    }
    public ShopItem upItem(String name, Long shopId, Long itemId, Double price){
        Shop shop=shopDao.getReferenceById(shopId);
        Item item=itemDao.getReferenceById(itemId);
        ShopItem shopItem=new ShopItem();
        shopItem.setItem(item);
        shopItem.setShop(shop);
        shopItem.setPrice(price);
        shopItemDao.save(shopItem);
        return shopItem;
    }
    public Item createItem(String name,String itemName){
        Business business=getDAO().findBusinessByName(name);
        Item item=new Item();
        item.setName(itemName);
        item.setBusiness(business);
        business.addItem(item);
        getDAO().save(business);
        return item;
    }
    @Transactional
    public Shop createShop(String name,String shopName){
        Business business=getDAO().findBusinessByName(name);
        Shop shop=new Shop();
        shop.setName(shopName);
        shop.setBusiness(business);
        business.addShop(shop);
        getDAO().save(business);
        return shop;
    }
    public Business register(String name, String password){
        Business business= getDAO().findBusinessByName(name);
        if(business==null){
            business=new Business();
            business.setName(name);
            business.setPassword(password);
            business.setUsername(name);
            getDAO().save(business);
        }else business=null;
        return business;
    }
    public Business login(String name,String password){
        Business business= getDAO().findBusinessByName(name);
        if(business!=null){
            Business business1=new Business();
            business1.setPassword(password);
            System.out.println(business.getPassword());
            System.out.println(business1.getPassword());
            if (!Objects.equals(business1.getPassword(), business.getPassword())){
                business=null;
            }
        }
        return business;
    }
}
